package com.neusoft.neuiotms.dcam.fixed.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.neusoft.bizcore.web.dto.result.PageResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcam.fixed.dto.ProductDTO;
import com.neusoft.neuiotms.dcam.fixed.service.ProductService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Api(tags = "固定资产物料管理")
@Slf4j
@RestController
@RequestMapping(value = "/api/w/fixed/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @ApiOperation("查询所有固定资产物料")
    @GetMapping
    public ResultListDTO<ProductDTO> index(final Searchable searchable) {
        final List<ProductDTO> productDTOs = this.productService.index(searchable);
        return ResultListDTO.success(productDTOs);
    }

    @ApiOperation("创建固定资产物料")
    @PostMapping
    public ResultDTO<ProductDTO> create(@RequestBody final ProductDTO dto) {
        final ProductDTO result = this.productService.create(dto);
        return ResultDTO.success(result);
    }

    @ApiOperation("固定资产物料详情")
    @GetMapping("/{id}")
    public ResultDTO<ProductDTO> details(@PathVariable final Long id) {
        final ProductDTO dto = this.productService.details(id);
        return ResultDTO.success(dto);
    }

    @ApiOperation("删除固定资产物料")
    @DeleteMapping("/{id}")
    public ResultDTO<Void> delete(@PathVariable final Long id) {
        this.productService.delete(id);
        return ResultDTO.success();
    }

    @ApiOperation("修改固定资产物料")
    @PutMapping("/{id}")
    public ResultDTO<ProductDTO> update(@PathVariable final Long id,
            @RequestBody final ProductDTO dto) {
        final ProductDTO result = this.productService.update(id, dto);
        return ResultDTO.success(result);
    }

    @ApiOperation("分页查询所有固定资产物料")
    @GetMapping("/p")
    public PageResultDTO<ProductDTO> search(final Pageable pageable, final Searchable searchable) {
        return PageResultDTO.success(this.productService.search(pageable, searchable));
    }

    @ApiOperation("下载物料导入模板")
    @GetMapping("/template")
    public void template(final HttpServletResponse response) {
        final Workbook workbook = this.productService.template();
        if (workbook == null) {
            ProductController.log.error("模板导出失败");
        }
        OutputStream out = null;
        try {
            out = response.getOutputStream();
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setHeader("Content-disposition",
                    String.format("attachment; filename=%s",
                            URLEncoder.encode("物料导入模板" + ".xlsx", "UTF-8")));
            workbook.write(out);
            out.flush();
        } catch (final Exception e) {
            ProductController.log.error("下载模板失败", e);
        } finally {
            try {
                out.close();
            } catch (final IOException e) {
                ProductController.log.error("下载模板关闭输出流失败", e);
            }
        }
    }

    @ApiOperation("导入物料")
    @PostMapping("/import")
    public ResultDTO<?> upload(@RequestParam("file") final MultipartFile file) {
        final String msg = this.productService.upload(file);
        if (!StringUtils.isEmpty(msg)) {
            return ResultDTO.failure(msg);
        }
        return ResultDTO.success(msg);
    }
}
